package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 注入service
	 * 
	 * @return
	 */
	@Autowired
	private CourierService courierService;

	@Action(value = "courier_save", results = {
			@Result(name = "save", type = "redirect", location = "./pages/base/courier.html") })
	public String save() {
		courierService.save(model);
		return "save";
	}

	@Action(value = "courier_pageQuery", results = { @Result(name = "json", type = "json") })
	public String pageQuery() {
		/*
		 * 无条件分页查询 //创建一个PageRequest封装分页请求参数 PageRequest pages = new
		 * PageRequest(page-1, rows);
		 * 
		 * //将分页请求参数传递给业务层,得到响应结果 Page<Courier> pageDate =
		 * courierService.findPageDate(pages);
		 * 
		 * //创建一个lmap集合用来存储响应的数据 Map<String,Object> result = new
		 * HashMap<String,Object>();
		 * 
		 * //将总记录数放入map集合中 result.put("total", pageDate.getTotalElements());
		 * 
		 * //将分页信息放入map集合中 result.put("rows", pageDate.getContent());
		 * 
		 * ServletActionContext.getContext().getValueStack().push(result);
		 */

		// 有条件分页查询
		PageRequest pageable = new PageRequest(page - 1, rows);

		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				// predicate拼装查询条件
				Predicate predicate = null;

				List<Predicate> list = new ArrayList<Predicate>();

				// 快递员工号
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p);
				}

				// 所属单位
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p);
				}
				// 快递员类型
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p = cb.like(root.get("type").as(String.class), "%" + model.getType() + "%");
					list.add(p);
				}

				// 收派标准(多表操作)
				Join<Courier, Standard> join = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && model.getStandard().getId() != null
						&& StringUtils.isNotBlank(model.getStandard().getId().toString())) {
					Predicate p = cb.equal(join.get("id").as(Integer.class), model.getStandard().getId());
					list.add(p);
				}

				// 构建条件的组合
				if (list != null && list.size() > 0) {
					// 构建一个数组
					Predicate p[] = new Predicate[list.size()];

					predicate = cb.and(list.toArray(p));
				}

				return predicate;
			}
		};

		Page<Courier> pageDate = courierService.findPageQueryByCondition(specification, pageable);

		super.pushPageDataToValueStack(pageDate);
		return "json";
	}

	@Action(value = "courier_deleteCourier", results = {
			@Result(name = "delectSuccess", type = "redirect", location = "./pages/base/courier.html") })
	public String deleteCourier() {
		courierService.deleteCourier(ids);
		return "delectSuccess";
	}

	@Action(value = "courier_recoverCourier", results = {
			@Result(name = "recoverSuccess", type = "redirect", location = "./pages/base/courier.html") })
	public String recoverCourier() {
		courierService.recoverCourier(ids);
		return "recoverSuccess";
	}

	@Action(value = "courier_findnoassociation", results = { @Result(name = "success", type = "json") })
	public String findnoassociation() {
		// 调用业务层，查询未关联定区的快递员
		List<Courier> couriers = courierService.findNoAssociation();
		// 将查询快递员列表 压入值栈
		ActionContext.getContext().getValueStack().push(couriers);

		return "success";
	}

	String fixedAreaId;

	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}

	// 查询关联快递员 列表
	@Action(value = "courier_findAssociationCourier", results = { @Result(name = "success", type = "json") })
	public String findAssociationCourier() {
		// 调用业务层，查询关联快递员 列表
		List<Courier> couriers = courierService.findAssociationCourier(fixedAreaId);
		// 将查询快递员列表 压入值栈
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}

}
